/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */


package com.smg.community.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javacommon.base.BaseRestSpringController;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;

import cn.org.rapid_framework.page.Page;

import com.smg.community.model.SmgGroup;
import com.smg.community.model.SmgGroupCategory;
import com.smg.community.model.SmgTopic;
import com.smg.community.setting.Cache;
import com.smg.community.setting.Constant;
import com.smg.community.util.ProjectUtil;
import com.smg.community.vo.query.SmgGroupQuery;
import com.smg.community.vo.query.SmgTopicQuery;
/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/activity")
public class ActivityController extends BaseRestSpringController<SmgTopic,java.lang.Integer>{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@RequestMapping("/list/{cityNo}-{cateId}-{filter}-{groupId}-{pageNumber}-{orderBy}")
	public    String   intoActivityList(HttpServletRequest request,ModelMap model,@PathVariable int cityNo,@PathVariable int cateId,@PathVariable int filter,@PathVariable int  groupId
			,@PathVariable int pageNumber,@PathVariable int orderBy){
		try {
		setCommunityCommonModel(request,model);
		setHeadTabIndex(model,Constant.ACTIVITY_TAB);
		//所有分类
				List<SmgGroupCategory>  categoryList = smgGroupCategoryManager.findAll() ;
				model.addAttribute("categoryList",categoryList);
				
				//头部8个小组 按主题数排序
				SmgGroupQuery groupQuery= new  SmgGroupQuery();
				groupQuery.setLimit(8);
				groupQuery.setFkCateId(cateId==0?null:cateId);
				groupQuery.setOrderBy("totalTopicCount desc");
				List<SmgGroup> eightGroupList  = smgGroupManager.getByQuery(groupQuery);
				model.addAttribute("eightGroupList",eightGroupList);
		//普通主题分页列表
		SmgTopicQuery query = new SmgTopicQuery();
		query.setCityNo(cityNo);
		query.setFkCateId(cateId == 0 ?null:cateId);
		String orderByString = setQueryTopicOrderbyString(orderBy);
		query.setOrderBy(orderByString);
		query.setPageNumber(pageNumber);
		query.setFkGroupId(groupId==0?null:groupId);
		query.setTopicType(Constant.TOPIC_TYPE_ACTIVITY);
		query.setPageSize(28);
		setStartAndLimit(query, pageNumber,28);
		// 帖子列表页  
		if ( cateId ==  0 ){
			//如果未选择分类，则列出所有非全站置顶帖
			query.setTopType("toptype !="+Constant.FULL_SIZT_TOP_TYPE);
		}else{
			//如果选择了某个分类
			query.setTopType("  toptype !="+Constant.FULL_SIZT_TOP_TYPE+" and toptype !="+Constant.CATEGORY_TOP_TYPE);
		}
		Page<SmgTopic> topicPage = smgTopicManager.findActivityPage(query);
		for(SmgTopic topic:topicPage) {
				ProjectUtil.filterTopic(topic);
		}
		model.addAttribute("topicPage",topicPage);
		//全站置顶帖
		List<SmgTopic> fullSiteTopList ;
		query = new SmgTopicQuery();
		query.setCityNo(cityNo);
		query.setTopType("toptype="+Constant.FULL_SIZT_TOP_TYPE);
		query.setStart(0);
		query.setLimit(100);
		fullSiteTopList = smgTopicManager.getByQuery(query);
		model.addAttribute("fullSiteTopList",fullSiteTopList);
		//分类置顶帖
		List<SmgTopic> cateGoryTopList ;
		query = new SmgTopicQuery();
		query = new SmgTopicQuery();
		query.setCityNo(cityNo);
		query.setTopType("toptype="+Constant.CATEGORY_TOP_TYPE);
		query.setFkCateId(cateId);
		query.setStart(0);
		query.setLimit(100);
		cateGoryTopList = smgTopicManager.getByQuery(query);
		model.addAttribute("cateGoryTopList",cateGoryTopList);
		
		model.addAttribute("topicTitleTypeList",Cache.TopicTitleTypeList);
		if ( cateId > 0 ){
			groupQuery= new  SmgGroupQuery();
			groupQuery.setLimit(800);
			groupQuery.setFkCateId(cateId);
			List<SmgGroup> allGroupList = smgGroupManager.getByQuery(groupQuery);
			model.addAttribute("allGroupList",allGroupList);
		}
		//页面查询参数
		model.addAttribute("cateId",cateId);
		model.addAttribute("cityNo",cityNo);
		model.addAttribute("filter",filter);
		model.addAttribute("pageNumber",pageNumber);
		model.addAttribute("orderBy",orderBy);
		model.addAttribute("groupId",groupId);
		}catch(Exception e){
			e.printStackTrace();
			return toErrorPage(model, "出错了...", e);
		}
		return "/activity/activitylist";
	}
	
	@RequestMapping("")
	public    String  intoShanghaiActivityList(HttpServletRequest request,ModelMap model){
		return intoActivityList(request,model,21,0,0,0,1,1);
	}
	@RequestMapping("/{cityNo}")
	public    String  intoCityActivityList(HttpServletRequest request,ModelMap model,@PathVariable int  cityNo){
		return intoActivityList(request,model,cityNo,0,0,0,1,1);
	}  
}

